#include "Mutex.h"

// Initialise mutex.
Mutex::Mutex() : mutex_() {
  pthread_mutex_init(&mutex_, NULL);
}

// Destroy mutex.
Mutex::~Mutex() {
  pthread_mutex_destroy(&mutex_);
}

// Lock the mutex. Blocks until mutex is unlocked.
void Mutex::lock() {
  pthread_mutex_lock(&mutex_);
}

// Attempt to lock the mutex. Returns false if mutex already locked.
bool Mutex::trylock() {
  return pthread_mutex_trylock(&mutex_) == 0;
}

// Unlock the mutex.
void Mutex::unlock() {
  pthread_mutex_unlock(&mutex_);
}

